package img

import (
	"image"
	"image/color"
	"image/draw"
	"io/ioutil"
	"math"

	"github.com/golang/freetype"
	"golang.org/x/image/font"

	"github.com/golang/freetype/truetype"
)

const (
	defFontFilePath = "./img/font/default.ttf"
)

// Brush 画笔
type Brush struct {
	FontType  *truetype.Font //字体样式
	FontSize  int            //字体大小
	FontColor Color          //字体颜色
}

// SetFontSize 设置画笔大小
func (this *Brush) SetFontSize(fontSize int) {
	this.FontSize = fontSize
}

// SetFontColor 设置画笔颜色
func (this *Brush) SetFontColor(color color.Color) {
	this.FontColor = newColor(color)
}

// SetFontTypeLocal 设置画笔样式本地
func (this *Brush) SetFontTypeLocal(path string) error {
	//读取字体文件
	font, err := ioutil.ReadFile(path)
	if err != nil {
		return err
	}
	//加载字体
	return this.SetFontType(font)
}

// SetFontType 设置画笔样式
func (this *Brush) SetFontType(font []byte) error {
	fontType, err := truetype.Parse(font)
	if err != nil {
		return err
	}
	this.FontType = fontType
	return nil
}

// WriteContent 写入内容
func (this *Brush) WriteContent(rgba draw.Image, start image.Point, content string) error {
	c := freetype.NewContext()
	c.SetDPI(45)
	c.SetFont(this.FontType)
	c.SetHinting(font.HintingFull)
	c.SetFontSize(float64(this.FontSize))
	c.SetClip(rgba.Bounds())
	c.SetDst(rgba)
	c.SetSrc(image.NewUniform(this.FontColor))
	_, err := c.DrawString(content, freetype.Pt(start.X, start.Y))
	return err
}

//========================= 图片绘画 =========================//

// DrawPoint 画点
func (this *Brush) DrawPoint(img draw.Image, site image.Point) {
	for x := site.X - this.FontSize; x < site.X+this.FontSize; x++ {
		for y := site.Y - this.FontSize; y < site.Y+this.FontSize; y++ {
			distance := math.Sqrt(math.Pow(float64(x-site.X), 2) + math.Pow(float64(y-site.Y), 2))
			if distance < float64(this.FontSize)-1 {
				img.Set(x, y, this.FontColor)
			} else if distance < float64(this.FontSize) {
				//TODO 锯齿处理
				img.Set(x, y, this.FontColor)
			}
		}
	}
}

// DrawLine 画线
func (this *Brush) DrawLine() {

}

// DrawCircle 画圆
func (this *Brush) DrawCircle(img draw.Image, centre image.Point, radii int) {
	for x := centre.X - radii; x < centre.X+radii; x++ {
		for y := centre.Y - radii; y < centre.Y+radii; y++ {
			distance := math.Sqrt(math.Pow(float64(x-centre.X), 2) + math.Pow(float64(y-centre.Y), 2))
			if distance >= float64(radii)-1 && distance < float64(radii) {
				img.Set(x, y, this.FontColor)
			}
		}
	}
}

// DrawRectangle 画正方形
func (this *Brush) DrawRectangle(img draw.Image, start image.Point, wide, high int) {
	for x := start.X; x <= start.X+wide; x++ {
		img.Set(x, start.Y, this.FontColor)
	}
	for x := start.X; x <= start.X+wide; x++ {
		img.Set(x, start.Y+high, this.FontColor)
	}
	for y := start.Y; y <= start.Y+high; y++ {
		img.Set(start.X, y, this.FontColor)
	}
	for y := start.Y; y <= start.Y+high; y++ {
		img.Set(start.X+wide, y, this.FontColor)
	}
}

//========================= 新建画笔 =========================//

//新建画笔
func newBrush() (*Brush, error) {
	//加载字体
	fontType, err := truetype.Parse(defFontBytes)
	if err != nil {
		return nil, err
	}
	return &Brush{
		FontType:  fontType,
		FontSize:  50,
		FontColor: newColor(color.Black),
	}, nil
}
